<?php
class PsalidasController extends AppController {

	var $name = 'Psalidas';
	var $helpers = array('Access');

	function index() {
		$this->Psalida->recursive = 0;
		$this->set('psalidas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid psalida', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('psalida', $this->Psalida->read(null, $id));
	}

	function add($participante = null) {
		if (!empty($this->data)) {
			$this->Psalida->create();
			if ($this->Psalida->save($this->data)) {
				$this->Session->setFlash(__('Prueba de Salida Guardada', true));
				$this->redirect(array('controller'=>'participantes','action' => 'index/'.$this->data['Psalida']['grupo_id']));
			} else {
				$this->Session->setFlash(__('The psalida could not be saved. Please, try again.', true));
			}
		}
		$participante = $this->Psalida->Participante->read(null, $participante);
		$this->set(compact('participante'));
	}

	function edit($participante = null) {
		if (!$participante && empty($this->data)) {
			$this->Session->setFlash(__('Invalid psalida', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Psalida->save($this->data)) {
				$this->Session->setFlash(__('The psalida has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The psalida could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Psalida->find('first',array('conditions'=>array('Psalida.participante_id'=>$participante)));
		}
		$puntaje_final = $this->data['Psalida']['puntaje_total'];
		$participante = $this->Psalida->Participante->read(null, $participante);
		$this->set(compact('participante','puntaje_final'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for psalida', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Psalida->delete($id)) {
			$this->Session->setFlash(__('Psalida deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Psalida was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
